package com.oracle.log.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.oracle.log.service.api.Api;
import com.oracle.log.service.api.LogServiceApi;
import com.oracle.mapper.LogsMapper;
import com.oracle.pojo.Logs;
import com.oracle.vo.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class LogServiceImpl implements LogServiceApi {

    @Autowired
    private LogsMapper logsMapper;

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @Override
    public ResultEntity addLogs(Logs logs) {
        if (logs.getAdmin() == null || "".equals(logs.getAdmin())) {
            return new ResultEntity(Api.LOG_CODE_NUADMIN, "不能没有管理员参数");
        }
        if (logs.getCreatedate() == null) {
            return new ResultEntity(Api.LOG_CODE_NUCREATETIME, "不能没有创建时间参数");
        }
        if (logs.getExecutemethod() == null || "".equals(logs.getExecutemethod())) {
            return new ResultEntity(Api.LOG_CODE_NUMETHOD, "不能没有执行方法参数");
        }
        if (logs.getExecutetype() == null || "".equals(logs.getExecutetype())) {
            return new ResultEntity(Api.LOG_CODE_NUTYPE, "不能没有执行的类型参数");
        }
        if (logs.getExecutetime() == null) {
            return new ResultEntity(Api.LOG_CODE_NUEXECUTETIME, "不能没有方法执行所用时间参数");
        }
        this.logsMapper.insertSelective(logs);
        return new ResultEntity(ResultEntity.CODE_SUCCESS, ResultEntity.MSG_SUCCESS);
    }

    @Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
    @Override
    public ResultEntity<List<Map<String, Object>>> getExecuteTypeAndCount() {
        List<Map<String, Object>> mapList = this.logsMapper.selectExecuteTypeAndCount();
        return new ResultEntity<List<Map<String, Object>>>(ResultEntity.CODE_SUCCESS, ResultEntity.MSG_SUCCESS, mapList);
    }
}
